//
//  111
//
//  Created by apple on 2021/10/26. 优化痛风分型bug 3.4.4
//  Copyright © 2021 zglee. All rights reserved.
//mj档案（知痛风）

#import "MemberInfoVC.h"
#import "MemerInfoCell.h"
#import "GoutRecordListHeaderCell.h"
#import "MemerInfoMultipleCell.h"
#import "MemberInfoHeadView.h"
#import "MyProfileController.h"
#import "MemberDetectionRecordVC.h"
#import "MJTotalDetectionResultVC.h"
#import "MJHemoglobinDetectionResultVC.h"
#import "MJBloodSugarDetectionResultVC.h"
#import "MJGoutDetectionResultVC.h"
#import "MJMultipleDetectionResultVC.h"
#import "AddMemberVC.h"

typedef NS_ENUM(NSUInteger, RDetectionItemType) {
    RDetectionItemTypeMultipleCell,
    RDetectionItemTypeGoutCell,
    RDetectionItemTypeGluCell,
    RDetectionItemTypeHBCell,
    RDetectionItemTypeTCCell,

};
@interface MemberInfoVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MJUITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) MemberInfoHeadView *tbvHeadView;
@property (nonatomic, strong) UIView *tbvNullFootView;

@property (nonatomic, strong) MJTotalCholesterolModel *totalCholesterolModel;
@property (nonatomic, strong) MJGoutDetectionModel *goutDetectionModel;
@property (nonatomic, strong) MJMultipleModel *multipleModel;
@property (nonatomic, strong) MJHemoglobinModel *hemoglobinModel;
@property (nonatomic, strong) MJBloodSugarModel *bloodSugarModel;
@property (nonatomic, assign) NSInteger loadDataCount;

@end

@implementation MemberInfoVC

- (instancetype)initWithPatientId:(NSString *)patientId {
    if (self = [super init]) {
       self.patientModel = [HealthHelper getpatientModel:patientId];
    }
    return self;
}

- (void)viewDidLoad{
    self.navType = NavTypeGray;
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    [self requestData];
    
    self.tbvHeadView.patientModel = self.patientModel;
    [self requestDetectionData];
}

- (void)mj_setUI{
    [super mj_setUI];
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleEN){
        self.mj_titleLocalizable = [NSString stringWithFormat:@"%@ %@",MJUILocalizedString(@"Profile of"),self.patientModel.patientName];
    }else{
        self.mj_titleLocalizable = [NSString stringWithFormat:@"%@%@",self.patientModel.patientName,MJUILocalizedString(@"Profile of")];
    }
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
     }];
    
    NSLog(@"patientModel=======================%@",self.patientModel.patientId);

}

#pragma mark - 方法
- (void)requestDetectionData{
    MJCommonModel *parms = [[MJCommonModel alloc]init];
    parms.patientId = self.patientModel.patientId;
    NSArray *multipleArray = [WCBDHelper.shared mj_getMultipleArrayWithParm:parms];
    if(multipleArray.count){
        self.multipleModel = multipleArray[0];
        self.multipleModel.total = multipleArray.count;
    }
    NSArray *goutArray = [WCBDHelper.shared mj_getGoutArrayWithParm:parms];
    if(goutArray.count){
        self.goutDetectionModel = goutArray[0];
        self.goutDetectionModel.total = goutArray.count;
    }
    NSArray *hemoglobinArray = [WCBDHelper.shared mj_getHemoglobinArrayWithParm:parms];
    if(hemoglobinArray.count){
        self.hemoglobinModel = hemoglobinArray[0];
        self.hemoglobinModel.total = hemoglobinArray.count;
    }
    NSArray *bloodSugarArray = [WCBDHelper.shared mj_getBloodSugarArrayWithParm:parms];
    if(bloodSugarArray.count){
        self.bloodSugarModel = bloodSugarArray[0];
        self.bloodSugarModel.total = bloodSugarArray.count;
    }
    NSArray *totalCholesterolArray = [WCBDHelper.shared mj_getTotalCholesterolArrayWithParm:parms];
    if(totalCholesterolArray.count){
        self.totalCholesterolModel = totalCholesterolArray[0];
        self.totalCholesterolModel.total = totalCholesterolArray.count;
    }
    [self updateUI];

}
- (void)requestData{
    self.loadDataCount = 0;
    MJWeakSelf
    NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{@"isMultiple":@1}];
    
    if(self.patientModel.patientId){
        [parms setValue:self.patientModel.patientId forKey:@"patientId"];
    }
    [MJHttpNetWorking requestWithParams:parms requestUri:MultipleDetection_select requestType:RequestTypeGet showLoading:!self.hideLoading success:^(MJBaseModel *model) {
        if (model.code ==  200){
            weakSelf.hideLoading = true;

            NSArray *data = model.data;
            if(data.count > 0){
                weakSelf.multipleModel = [MJMultipleModel yy_modelWithDictionary:data[0]];
                weakSelf.multipleModel.total = data.count;
            }
            weakSelf.loadDataCount++;
            [weakSelf updateUI];
        }
    } failure:^(NSError *error) {
        
    }];
    [MJHttpNetWorking requestWithParams:parms requestUri:GoutDetection_select requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if (model.code ==  200){
            NSArray *data = model.data;
            if(data.count > 0){
                weakSelf.goutDetectionModel = [MJGoutDetectionModel yy_modelWithDictionary:data[0]];
                weakSelf.goutDetectionModel.total = data.count;
            }
            weakSelf.loadDataCount++;
            [weakSelf updateUI];
        }
    } failure:^(NSError *error) {
        
    }];
    [MJHttpNetWorking requestWithParams:parms requestUri:HemoglobinDetection_select requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if (model.code ==  200){
            NSArray *data = model.data;
            if(data.count > 0){
                weakSelf.hemoglobinModel = [MJHemoglobinModel yy_modelWithDictionary:data[0]];
                weakSelf.hemoglobinModel.total = data.count;
            }
            weakSelf.loadDataCount++;
            [weakSelf updateUI];
        }
    } failure:^(NSError *error) {
        
    }];
    [MJHttpNetWorking requestWithParams:parms requestUri:BloodGlucoseDetection_select requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if (model.code ==  200){
            NSArray *data = model.data;
            if(data.count > 0){
                weakSelf.bloodSugarModel = [MJBloodSugarModel yy_modelWithDictionary:data[0]];
                weakSelf.bloodSugarModel.total = data.count;
            }
            weakSelf.loadDataCount++;
            [weakSelf updateUI];
        }
    } failure:^(NSError *error) {
        
    }];
    [MJHttpNetWorking requestWithParams:parms requestUri:TotalCholesterolDetection_select requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        if (model.code ==  200){
            NSArray *data = model.data;
            if(data.count > 0){
                weakSelf.totalCholesterolModel = [MJTotalCholesterolModel yy_modelWithDictionary:data[0]];
                weakSelf.totalCholesterolModel.total = data.count;
            }
            weakSelf.loadDataCount++;
            [weakSelf updateUI];
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)updateUI{
    [self.tableView reloadData];
    
    if(!self.multipleModel && !self.goutDetectionModel && !self.bloodSugarModel && !self.hemoglobinModel && !self.totalCholesterolModel){
        self.tableView.tableFooterView =  self.tbvNullFootView;
    }else{
        self.tableView.tableFooterView =  nil;
    }
}

- (void)processData:(NSArray *)modelArray{
    
    if(modelArray.count){
        self.dataArray = [NSMutableArray arrayWithArray:modelArray];
        self.goutDetectionModel = self.dataArray[0];
        self.bloodSugarModel = self.dataArray[0];
        self.hemoglobinModel = self.dataArray[0];
        self.totalCholesterolModel = self.dataArray[0];
        self.multipleModel = self.dataArray[0];
        self.tableView.tableFooterView =  nil;
    }else{
        self.tableView.tableFooterView =  self.tbvNullFootView;
    }
    [self.tableView reloadData];
}

- (void)jumpMemberDetectionRecord:(NSInteger)index{
    MemberDetectionRecordVC *vc = [[MemberDetectionRecordVC alloc]init];
    vc.index = index;
    vc.patientModel = self.patientModel;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == RDetectionItemTypeMultipleCell && !self.multipleModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeGoutCell && !self.goutDetectionModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeGluCell && !self.bloodSugarModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeHBCell && !self.hemoglobinModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeTCCell && !self.totalCholesterolModel){
         return 0;
    }
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MJWeakSelf
    if(indexPath.row == RDetectionItemTypeMultipleCell){
        if(!self.multipleModel){return [UITableViewCell mj_initNullStyle];}
        MemerInfoMultipleCell *cell = [MemerInfoMultipleCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.multipleModel = self.multipleModel;
        [cell.topView.itemButton mj_addAction:^(UIButton *button) {
            [weakSelf jumpMemberDetectionRecord:0];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeGoutCell){
        if(!self.goutDetectionModel){return [UITableViewCell mj_initNullStyle];}
        MemerInfoCell *cell = [MemerInfoCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.goutDetectionModel = self.goutDetectionModel;
        [cell.topView.itemButton mj_addAction:^(UIButton *button) {
            [weakSelf jumpMemberDetectionRecord:1];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeGluCell){
        if(!self.bloodSugarModel){return [UITableViewCell mj_initNullStyle];}
        MemerInfoCell *cell = [MemerInfoCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.bloodSugarModel = self.bloodSugarModel;
        [cell.topView.itemButton mj_addAction:^(UIButton *button) {
            [weakSelf jumpMemberDetectionRecord:2];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeHBCell){
        if(!self.hemoglobinModel){return [UITableViewCell mj_initNullStyle];}
        MemerInfoCell *cell = [MemerInfoCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.hemoglobinModel = self.hemoglobinModel;
        [cell.topView.itemButton mj_addAction:^(UIButton *button) {
            [weakSelf jumpMemberDetectionRecord:4];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeTCCell){
        if(!self.totalCholesterolModel){return [UITableViewCell mj_initNullStyle];}
        MemerInfoCell *cell = [MemerInfoCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.totalCholesterolModel = self.totalCholesterolModel;
        [cell.topView.itemButton mj_addAction:^(UIButton *button) {
            [weakSelf jumpMemberDetectionRecord:3];
        }];
        return cell;
    }
    return [UITableViewCell mj_initNullStyle];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == RDetectionItemTypeMultipleCell){
        MJMultipleDetectionResultVC *vc = [[MJMultipleDetectionResultVC alloc]initWithModel:self.multipleModel];
        [self.navigationController pushViewController:vc animated:true];
    }
    if(indexPath.row == RDetectionItemTypeGoutCell){
        MJGoutDetectionResultVC *vc = [[MJGoutDetectionResultVC alloc]initWithModel:self.goutDetectionModel];
        [self.navigationController pushViewController:vc animated:true];
    }
    if(indexPath.row == RDetectionItemTypeGluCell){
        MJBloodSugarDetectionResultVC *vc = [[MJBloodSugarDetectionResultVC alloc]initWithModel:self.bloodSugarModel];
        [self.navigationController pushViewController:vc animated:true];
    }
    if(indexPath.row == RDetectionItemTypeHBCell){
        MJHemoglobinDetectionResultVC *vc = [[MJHemoglobinDetectionResultVC alloc]initWithModel:self.hemoglobinModel];
        [self.navigationController pushViewController:vc animated:true];
    }
    if(indexPath.row == RDetectionItemTypeTCCell){
        MJTotalDetectionResultVC *vc = [[MJTotalDetectionResultVC alloc]initWithModel:self.totalCholesterolModel];
        [self.navigationController pushViewController:vc animated:true];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (MJUITableView *)tableView{
    if (!_tableView){
        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 150;
        tableView.contentInset= UIEdgeInsetsMake(0,0, 10, 0);
        [MemerInfoCell registerNibWithTableView:tableView];
        [MemerInfoMultipleCell registerNibWithTableView:tableView];
        tableView.tableHeaderView = self.tbvHeadView;
        _tableView = tableView;
    }
    return _tableView;
}
- (MemberInfoHeadView *)tbvHeadView {
    if (!_tbvHeadView) {
        MJWeakSelf
        _tbvHeadView = [MemberInfoHeadView mj_loadNibWithOwner:self];
        _tbvHeadView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 275);
        _tbvHeadView.autoresizingMask = UIViewAutoresizingNone;
        [_tbvHeadView.infoButton mj_addAction:^(UIButton *button) {
            AddMemberVC *vc = [[AddMemberVC alloc]init];
            vc.patientModel = weakSelf.patientModel;
            vc.handlePatientModelBlock = ^(PatientModel *patientModel) {
                weakSelf.patientModel = patientModel;
                [WCBDHelper.shared mj_initPatientArray];
            };
            [weakSelf.navigationController pushViewController:vc animated:true];
        }];
        [_tbvHeadView.detectionButton mj_addAction:^(UIButton *button) {
            [HealthHelper jumpDetectionViewControllerWith:weakSelf.patientModel viewController:weakSelf];
        }];
        
    }
    return _tbvHeadView;
}

- (UIView *)tbvNullFootView {
    if (!_tbvNullFootView) {
        _tbvNullFootView = [[UIView alloc]init];
        _tbvNullFootView.frame = CGRectMake(0, 0, SCREEN_WIDTH, KVIEW_HEIGHT - 280);
        _tbvNullFootView.backgroundColor = UIColor.mj_backgroundColor;
        [self showEmptyView:_tbvNullFootView emptyStyle:EmptyViewMemberInfo];
    }
    return _tbvNullFootView;
}
@end
